package org.zhxy.core.personnel.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zhxy.common.base.BaseService;
import org.zhxy.common.config.MapperFactory;
import org.zhxy.common.error.CommonException;
import org.zhxy.common.response.PageResult;
import org.zhxy.common.response.Result;
import org.zhxy.common.response.ResultCode;
import org.zhxy.common.utils.CollectionUtils;
import org.zhxy.common.utils.Constants;
import org.zhxy.entity.personnel.*;
import org.zhxy.entity.system.User;

import java.util.*;

@Service
@Transactional
public class TeacherService extends BaseService {

    public PageResult<UserTeacher> searchTeacherList(Map<String, Object> map) throws CommonException {
        map.put(Constants.Blanket.PAGE,(Integer.parseInt(map.get(Constants.Blanket.PAGE).toString()) - Constants.Number.ONE) *
                Integer.parseInt(map.get(Constants.Blanket.SIZE).toString()));
        map.put(Constants.Blanket.SIZE,Integer.parseInt(map.get(Constants.Blanket.SIZE).toString()));
        switch (currentUser().getLevel()) {
            case Constants.System.ADMIN:
                List<UserTeacher> userTeachersList = MapperFactory.teacherMapper.searchAllUserTeacherList(map);
                long total = MapperFactory.teacherMapper.searchTotalCount(map);
                return new PageResult<>(total,userTeachersList);
            case Constants.System.USER:
                map.put(Constants.DB.ID,currentUser().getId());
                List<UserTeacher> userTeachers = MapperFactory.teacherMapper.searchUserTeacherList(map);
                long count = MapperFactory.teacherMapper.searchCountByTeacher(map);
                return new PageResult<>(count,userTeachers);
            default:
                throw new CommonException(ResultCode.FAIL);
        }
    }


    public List<TeacherDepartment> searchTeachersByDepartment(String departmentId) {
        QueryWrapper<TeacherDepartment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Constants.DB.DEPARTMENT_ID,departmentId);
        queryWrapper.eq(Constants.DB.STATUS, Constants.CharacterNumber.ONE);
        return MapperFactory.teacherDepartmentMapper.selectList(queryWrapper);
    }

    public UserTeacher searchTeacher(Map<String, Object> map) {
        UserTeacher userTeacher = new UserTeacher();
        Object idCard = map.get(Constants.Parameters.ID_CARD);
        if(idCard != null && !"".equals(idCard.toString())){
            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq(Constants.DB.ID_CARD,idCard);
            User user = MapperFactory.userMapper.selectOne(userQueryWrapper);
            if(user != null){
                BeanUtils.copyProperties(user,userTeacher);
                Teacher teacher = MapperFactory.teacherMapper.selectById(user.getId());
                BeanUtils.copyProperties(teacher,userTeacher);
            }
        }
        return userTeacher;
    }

    public Result updateTeacher(UserTeacher userTeacher) {
        userTeacher.setIdentityName(MapperFactory.dictionaryMapper.selectById(userTeacher.getIdentityId()).getName());
        String id = userTeacher.getId();
        User user = MapperFactory.userMapper.selectById(id);
        Teacher teacher = MapperFactory.teacherMapper.selectById(id);
        if(user == null && teacher == null){
            user = new User();
            BeanUtils.copyProperties(userTeacher,user);
            user.setPassword(new Md5Hash(userTeacher.getPassword(),userTeacher.getUsername(),3).toString());
            user.setId(id);
            MapperFactory.userMapper.insert(user);

            teacher = new Teacher();
            BeanUtils.copyProperties(userTeacher,teacher);
            teacher.setId(id);
            MapperFactory.teacherMapper.insert(teacher);

            //初始化转正记录
            TeacherGeneral teacherGeneral = new TeacherGeneral();
            teacherGeneral.setUserId(id);
            teacherGeneral.setGeneralStatus(Constants.CharacterNumber.ONE);
            MapperFactory.teacherGeneralMapper.insert(teacherGeneral);
            //初始化离职
            TeacherResignation teacherResignation = new TeacherResignation();
            teacherResignation.setUserId(id);
            teacherResignation.setResignationStatus(Constants.CharacterNumber.ONE);
            MapperFactory.teacherResignationMapper.insert(teacherResignation);
        }else{
            BeanUtils.copyProperties(userTeacher,user);
            MapperFactory.userMapper.updateById(user);
            BeanUtils.copyProperties(userTeacher,teacher);
            MapperFactory.teacherMapper.updateById(teacher);
        }
        return Result.SUCCESS();
    }

    public UserTeacher searchTeacherBySelf() {
        String userId = currentUser().getId();
        return MapperFactory.userMapper.searchTeacherBySelf(userId);
    }

    public Map<String, Object> searchTeachers() {
        Map<String, Object> map = new HashMap<>();
        List<Teacher> jyList = new ArrayList<>();
        List<Teacher> bzrList = new ArrayList<>();
        List<Teacher> teachers = MapperFactory.teacherMapper.selectList(null);
        if(CollectionUtils.checkCollection(teachers)){
            teachers.forEach(teacher -> {
                if(teacher.getIdentityName().equals(Constants.Chinese.BZR)){
                    bzrList.add(teacher);
                }
                if(teacher.getIdentityName().equals(Constants.Chinese.JY)){
                    jyList.add(teacher);
                }
            });
        }
        map.put(Constants.Parameters.JY_LIST,jyList);
        map.put(Constants.Parameters.BZR_LIST,bzrList);
        return map;
    }
}
